<template>
  <div>
    <Activity
      :receivablePlanValue="receivablePlanValue"
      @getReceivablePlanValue="getReceivablePlanValue"
      @closeDrawer="closeDrawer"
      @refreshPage="emit('refreshData')"
      @prev="prev"
      @next="next"
      :isLoad="isLoading"
      :isPool="isPool"
      :validateWrite="validateWrite"
      :validateOwnerUser="validateOwnerUser"
    />
    <!-- 详细资料 -->
    <Details v-if="selectTag == 0" :receivablePlanValue="receivablePlanValue" />
    <System v-if="selectTag == 0" :receivablePlanValue="receivablePlanValue" />
    <!-- 团队成员 -->
    <teamMembers
      v-if="selectTag == 1"
      :bizId="receivablePlanValue.id"
      :bizType="BizTypeEnum.CRM_RECEIVABLE_PLAN"
      :showAction="true"
    />

    <!-- 操作日志 -->
    <Log
      v-if="selectTag == 2"
      :clueValue="receivablePlanValue"
      :id="receivablePlanValue.id"
      :bizType="BizTypeEnum.CRM_RECEIVABLE_PLAN"
    />
  </div>
</template>

<script setup>
import { BizTypeEnum } from '@/api/crm/permission'
import Activity from './activity.vue'
import * as PermissionApi from '@/api/crm/permission'
import { useUserStoreWithOut } from '@/store/modules/user'
import teamMembers from '@/views/crm/components/teamMembers/index.vue'
import Log from '@/views/crm/components/log/index.vue'
import Details from './details.vue'
import System from './system.vue'
import * as ReceivablePlanApi from '@/api/crm/receivable/plan'

const isLoading = ref(false)
const receivablePlanValue = ref({})
const receivablePlanId = ref('')
const props = defineProps({
  selectTag: Number,
  id: String,
  nowIds: Array
})
const emit = defineEmits(['setDrawer', 'refreshData'])

const teamMembersList = ref([])
const loading = ref(false)
/** 查询列表 */
const getTeamMembers = async () => {
  loading.value = true
  try {
    const data = await PermissionApi.getPermissionList({
      bizType: BizTypeEnum.CRM_RECEIVABLE_PLAN,
      bizId: props.id
    })
    teamMembersList.value = data
  } finally {
    loading.value = false
  }
}
const getReceivablePlanValue = async () => {
  try {
    isLoading.value = true
    const res = await ReceivablePlanApi.getReceivablePlan(receivablePlanId.value)
    receivablePlanValue.value = res
  } finally {
    isLoading.value = false
  }
}
/**上一条*/
const prev = () => {
  const index = props.nowIds.indexOf(receivablePlanId.value) - 1
  if (props.nowIds[index]) {
    receivablePlanId.value = props.nowIds[index]
    getReceivablePlanValue()
    getTeamMembers()
  } else {
    ElMessage.error('没有更多了')
  }
}
/**下一条*/
const next = () => {
  const index = props.nowIds.indexOf(receivablePlanId.value) + 1
  if (props.nowIds[index]) {
    receivablePlanId.value = props.nowIds[index]
    getReceivablePlanValue()
    getTeamMembers()
  } else {
    ElMessage.error('没有更多了')
  }
}
watch(
  () => props.id,
  (val) => {
    if (val) {
      receivablePlanId.value = val
      getReceivablePlanValue()
      getTeamMembers()
    }
  },
  { deep: true, immediate: true }
)
/**关闭*/
const closeDrawer = () => {
  emit('setDrawer')
}
// 校验负责人权限和编辑权限
const validateOwnerUser = ref(false)
const validateWrite = ref(false)
const isPool = ref(false)
const userStore = useUserStoreWithOut()

watch(
  () => teamMembersList.value,
  (newArr) => {
    isPool.value = false
    validateOwnerUser.value = false
    validateWrite.value = false
    if (newArr.length > 0) {
      isPool.value = !newArr.some((item) => item.level === PermissionApi.PermissionLevelEnum.OWNER)
      const userId = userStore.getUser?.id
      newArr
        .filter((item) => item.userId === userId)
        .forEach((item) => {
          if (item.level === PermissionApi.PermissionLevelEnum.OWNER) {
            validateOwnerUser.value = true
            validateWrite.value = true
          } else if (item.level === PermissionApi.PermissionLevelEnum.WRITE) {
            validateWrite.value = true
          }
        })
    } else {
      isPool.value = true
    }
  },
  {
    immediate: true,
    deep: true
  }
)
</script>

<style lang="scss" scoped></style>
